<template>
  <div class="p-2">
    <div class="mb-[10px]" v-show="showSearch">
      <el-card shadow="hover">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="70">
          <el-form-item label="项目名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable style="width: 200px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable style="width: 200px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              v-hasPermi="['business:winTheBidding:register:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
              v-hasPermi="['business:winTheBidding:register:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['business:winTheBidding:register:remove']">
              删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['business:winTheBidding:register:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="regisiterList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.minuteBidBagList" style="width: 100%;margin:0 auto">
              <el-table-column prop="bagName" label="包名称" min-width="180" />
              <el-table-column prop="productName" label="产品名称" min-width="180" />
              <el-table-column prop="price" label="中标金额(万元)" min-width="180" />
              <el-table-column prop="year" label="年度" min-width="180" />
              <el-table-column prop="provinceName" label="省份" min-width="180" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="name" />
        <el-table-column label="中标次数" align="center" prop="wbCount" />
        <el-table-column label="中标总金额(万元)" align="center" prop="totalAmount" />
        <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="postFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="中标次数" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入中标次数" />
        </el-form-item>
        <el-form-item label="中标总金额" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <!-- <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="winTheBiddingRegisiter">
import { listRegisiter, addRegisiter, delRegisiter, getRegisiter, updateRegisiter } from "@/api/business/winTheBidding/register";
import { ElMessage, ElMessageBox } from 'element-plus'
import { toRefs, ref, reactive, onMounted, nextTick } from "vue";
const pageName = "中标信息登记"
// 中标数据
const regisiterList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const postFormRef = ref();
const queryFormRef = ref();

const dialog = reactive({
  visible: false,
  title: ''
});

const initFormData = {
  idArray: undefined,
  postCode: '',
  postName: '',
  postSort: 0,
  status: "0",
  remark: ''
}

const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productName: '',
    name: '',
  },
  rules: {
    postName: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "中标次数不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "中标总金额不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询岗位列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRegisiter(queryParams.value);
  regisiterList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  postFormRef.value?.resetFields();
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加中标信息";
}
/** 修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const idArray = row?.id || ids.value[0];
  const res = await getRegisiter(idArray);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改中标信息";
}
/** 提交按钮 */
const submitForm = () => {
  postFormRef.value?.validate(async (valid) => {
    if (valid) {
      form.value.idArray ? await updateRegisiter(form.value) : await addRegisiter(form.value);
      dialog.visible = false;
      await getList();
    }
  });
}
/** 删除按钮操作 */
const handleDelete = async (row) => {
  ElMessageBox.confirm(
    '确定删除吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const idArray = row?.id || ids.value;
      const res = delRegisiter(idArray);
      if (res.code == 200) {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getList();
      }
    })
    .catch(() => {
    })

}
/** 导出按钮操作 */
const handleExport = () => {
  // proxy?.download("system/post/export", {
  //   ...queryParams.value
  // }, `post_${new Date().getTime()}.xlsx`);
}

onMounted(() => {
  getList();
});
</script>
